<template>
  <div class="app-container">
    <el-form :model="queryParams" v-if="false" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px" size="medium"
             class="ry_form"
    >
      <el-form-item label="城市名" prop="cityName">
        <el-input
          v-model.trim="queryParams.cityName"
          placeholder="请输入城市名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="城市是否展示" prop="cityShow">
        <el-select v-model="queryParams.cityShow" placeholder="请选择城市是否展示" clearable size="small">
          <el-option
            v-for="dict in dict.type.field_show"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model.trim="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否显示产品名称" prop="productShow">
        <el-select v-model="queryParams.productShow" placeholder="请选择是否显示产品名称" clearable size="small">
          <el-option
            v-for="dict in dict.type.field_show"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="重量名称" prop="weightName">
        <el-input
          v-model.trim="queryParams.weightName"
          placeholder="请输入重量名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="重量是否显示" prop="weightShow">
        <el-select v-model="queryParams.weightShow" placeholder="请选择重量是否显示" clearable size="small">
          <el-option
            v-for="dict in dict.type.field_show"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="年龄字段" prop="oldName">
        <el-input
          v-model.trim="queryParams.oldName"
          placeholder="请输入年龄字段"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <template v-if="showMoreCondition">
        <el-form-item label="年龄字段是否显示" prop="oldShow">
          <el-select v-model="queryParams.oldShow" placeholder="请选择年龄字段是否显示" clearable size="small">
            <el-option
              v-for="dict in dict.type.field_show"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="身高字段" prop="heightName">
          <el-input
            v-model.trim="queryParams.heightName"
            placeholder="请输入身高字段"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="身高是否显示" prop="heightShow">
          <el-select v-model="queryParams.heightShow" placeholder="请选择身高是否显示" clearable size="small">
            <el-option
              v-for="dict in dict.type.field_show"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="罩杯字段" prop="cupsName">
          <el-input
            v-model.trim="queryParams.cupsName"
            placeholder="请输入罩杯字段"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="罩杯字段是否显示" prop="cupsShow">
          <el-select v-model="queryParams.cupsShow" placeholder="请选择罩杯字段是否显示" clearable size="small">
            <el-option
              v-for="dict in dict.type.field_show"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="其它字段" prop="otherName">
          <el-input
            v-model.trim="queryParams.otherName"
            placeholder="请输入其它字段"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="其它字段是否显示" prop="otherShow">
          <el-select v-model="queryParams.otherShow" placeholder="请选择其它字段是否显示" clearable size="small">
            <el-option
              v-for="dict in dict.type.field_show"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="民族字段" prop="ethnicGroup">
          <el-input
            v-model.trim="queryParams.ethnicGroup"
            placeholder="请输入ETHNIC_GROUP"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="民族字段是否显示" prop="ethnicGroupShow">
          <el-select v-model="queryParams.ethnicGroupShow" placeholder="请选择ETHNIC_GROUP_SHOW" clearable size="small">
            <el-option
              v-for="dict in dict.type.field_show"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </template>
      <el-form-item class="flex_one tr">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button :icon="showMoreCondition ? 'el-icon-arrow-up' : 'el-icon-arrow-down'" size="mini"
                   @click="showMoreCondition = !showMoreCondition"
        >{{ showMoreCondition ? '收起条件' : '展开条件' }}
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="PmsFieldList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="城市名" align="center" prop="cityName">
        <template slot-scope="scope">
          <router-link :to="'/system/dict-data/index/119'" class="link-type">
            <span>{{ scope.row.cityName }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column label="城市是否展示" align="center" prop="cityShow">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.field_show" :value="scope.row.cityShow"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="产品名称" align="center" prop="productName"/>-->
<!--      <el-table-column label="是否显示产品名称" align="center" prop="productShow">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.field_show" :value="scope.row.productShow"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="重量名称" align="center" prop="weightName">
        <template slot-scope="scope">
          <router-link :to="'/system/dict-data/index/122'" class="link-type">
            <span>{{ scope.row.weightName }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="重量是否显示" align="center" prop="weightShow">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.field_show" :value="scope.row.weightShow"/>
        </template>
      </el-table-column>
      <el-table-column label="年龄字段" align="center" prop="oldName">
        <template slot-scope="scope">
          <router-link :to="'/system/dict-data/index/121'" class="link-type">
            <span>{{ scope.row.oldName }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="年龄字段是否显示" align="center" prop="oldShow">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.field_show" :value="scope.row.oldShow"/>
        </template>
      </el-table-column>
      <el-table-column label="身高字段" align="center" prop="heightName">
        <template slot-scope="scope">
          <router-link :to="'/system/dict-data/index/120'" class="link-type">
            <span>{{ scope.row.heightName }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="身高是否显示" align="center" prop="heightShow">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.field_show" :value="scope.row.heightShow"/>
        </template>
      </el-table-column>
      <el-table-column label="罩杯字段" align="center" prop="cupsName">
        <template slot-scope="scope">
          <router-link :to="'/system/dict-data/index/117'" class="link-type">
            <span>{{ scope.row.cupsName }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="罩杯字段是否显示" align="center" prop="cupsShow">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.field_show" :value="scope.row.cupsShow"/>
        </template>
      </el-table-column>
      <el-table-column label="其它字段" align="center" prop="otherName">
        <template slot-scope="scope">
          <router-link :to="'/system/dict-data/index/118'" class="link-type">
            <span>{{ scope.row.otherName }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="其它字段是否显示" align="center" prop="otherShow">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.field_show" :value="scope.row.otherShow"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改字段表对话框 -->
    <el-dialog :title="title" :visible.sync="open" :width="withI" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="108px" inline class="dialog-form-two">
        <el-form-item label="城市名" prop="cityName">
          <el-input v-model.trim="form.cityName" placeholder="请输入城市名"/>
        </el-form-item>
        <el-form-item label="城市是否展示">
          <el-radio-group v-model="form.cityShow">
            <el-radio
              v-for="dict in dict.type.field_show"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="产品名称" prop="productName">
          <el-input v-model.trim="form.productName" placeholder="请输入产品名称"/>
        </el-form-item>
        <el-form-item label="是否显示产品名称">
          <el-radio-group v-model="form.productShow">
            <el-radio
              v-for="dict in dict.type.field_show"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="重量名称" prop="weightName">
          <el-input v-model.trim="form.weightName" placeholder="请输入重量名称"/>
        </el-form-item>
        <el-form-item label="重量是否显示">
          <el-radio-group v-model="form.weightShow">
            <el-radio
              v-for="dict in dict.type.field_show"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄字段" prop="oldName">
          <el-input v-model.trim="form.oldName" placeholder="请输入年龄字段"/>
        </el-form-item>
        <el-form-item label="年龄字段是否显示">
          <el-radio-group v-model="form.oldShow">
            <el-radio
              v-for="dict in dict.type.field_show"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="身高字段" prop="heightName">
          <el-input v-model.trim="form.heightName" placeholder="请输入身高字段"/>
        </el-form-item>
        <el-form-item label="身高是否显示">
          <el-radio-group v-model="form.heightShow">
            <el-radio
              v-for="dict in dict.type.field_show"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="罩杯字段" prop="cupsName">
          <el-input v-model.trim="form.cupsName" placeholder="请输入罩杯字段"/>
        </el-form-item>
        <el-form-item label="罩杯字段是否显示">
          <el-radio-group v-model="form.cupsShow">
            <el-radio
              v-for="dict in dict.type.field_show"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="其它字段" prop="otherName">
          <el-input v-model.trim="form.otherName" placeholder="请输入其它字段"/>
        </el-form-item>
        <el-form-item label="其它字段是否显示">
          <el-radio-group v-model="form.otherShow">
            <el-radio
              v-for="dict in dict.type.field_show"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
<!--        <el-form-item label="民族字段" prop="ethnicGroup">-->
<!--          <el-input v-model.trim="form.ethnicGroup" placeholder="请输入ETHNIC_GROUP"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="民族字段是否显示">-->
<!--          <el-radio-group v-model="form.ethnicGroupShow">-->
<!--            <el-radio-->
<!--              v-for="dict in dict.type.field_show"-->
<!--              :key="dict.value"-->
<!--              :label="parseInt(dict.value)"-->
<!--            >{{ dict.label }}-->
<!--            </el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPmsField,
  getPmsField,
  delPmsField,
  addPmsField,
  updatePmsField,
  exportPmsField
} from '@/api/mall/pmsField'

export default {
  name: 'PmsField',
  dicts: ['field_show', 'field_show', 'field_show', 'field_show', 'field_show', 'field_show', 'field_show', 'field_show'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字段表表格数据
      PmsFieldList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cityName: null,
        cityShow: null,
        productName: null,
        productShow: null,
        weightName: null,
        weightShow: null,
        oldName: null,
        oldShow: null,
        heightName: null,
        heightShow: null,
        cupsName: null,
        cupsShow: null,
        otherName: null,
        otherShow: null,
        ethnicGroup: null,
        ethnicGroupShow: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      columns: [
        { key: 1, label: '城市名', visible: true },
        { key: 2, label: '城市是否展示', visible: true },
        { key: 3, label: '产品名称', visible: true },
        { key: 4, label: '是否显示产品名称', visible: true },
        { key: 5, label: '重量名称', visible: true },
        { key: 6, label: '重量是否显示', visible: true },
        { key: 7, label: '年龄字段', visible: true },
        { key: 8, label: '年龄字段是否显示', visible: true },
        { key: 9, label: '身高字段', visible: true },
        { key: 10, label: '身高是否显示', visible: false },
        { key: 11, label: '罩杯字段', visible: false },
        { key: 12, label: '罩杯字段是否显示', visible: false },
        { key: 13, label: '其它字段', visible: false },
        { key: 14, label: '其它字段是否显示', visible: false },
        { key: 15, label: '民族字段', visible: false },
        { key: 16, label: '民族字段是否显示', visible: false }
      ],
      showMoreCondition: false,
      withI: '50%' // 弹出层宽度
    }
  },
  created() {

    if (this.getDeviceType()) {
      this.withI = '88%'
    } else {
      this.withI = '50%'
    }
    this.getList()
  },
  methods: {
    getDeviceType() {
      // 优先宽度判断
      return window.innerWidth < 768;
    },
    /** 查询字段表列表 */
    getList() {
      this.loading = true
      const { pageNum, pageSize } = this.queryParams
      const query = { ...this.queryParams, pageNum: undefined, pageSize: undefined }
      const pageReq = { page: pageNum - 1, size: pageSize }
      listPmsField(query, pageReq).then(response => {
        const { content, totalElements } = response
        this.PmsFieldList = content
        this.total = totalElements
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cityName: null,
        cityShow: 0,
        productName: null,
        productShow: 0,
        weightName: null,
        weightShow: 0,
        oldName: null,
        oldShow: 0,
        heightName: null,
        heightShow: 0,
        cupsName: null,
        cupsShow: 0,
        otherName: null,
        otherShow: 0,
        ethnicGroup: null,
        ethnicGroupShow: 0
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加字段表'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getPmsField(id).then(response => {
        this.form = response
        this.open = true
        this.title = '修改字段表'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePmsField(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addPmsField(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除字段表编号为"' + ids + '"的数据项？').then(function() {
        return delPmsField(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal.confirm('是否确认导出所有字段表数据项？').then(() => {
        this.exportLoading = true
        return exportPmsField(queryParams)
      }).then(response => {
        this.$download.download(response)
        this.exportLoading = false
      }).catch(() => {
      })
    }
  }
}
</script>
